package com.lanou.sinasports.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.sinasports.R;
import com.lanou.sinasports.beans.GsonNewsRecOneDec;
import com.lanou.sinasports.utils.DoubleCache;
import com.lanou.sinasports.utils.GsonRequest;
import com.lanou.sinasports.utils.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/16.
 */
public class NewsRecDetOneActivity extends AppCompatActivity {
    private RequestQueue queue;
    private String url="";
    private TextView tvCount;
    private TextView tvTitle;
    private TextView tvDate;
    private TextView tvMedia;
    private NetworkImageView imageView;
    private TextView tvContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_rec_one_detail);
        tvCount= (TextView) findViewById(R.id.tv_new_rec_one_comment_count);
        tvTitle= (TextView) findViewById(R.id.tv_new_rec_one_the);
        tvDate= (TextView) findViewById(R.id.tv_new_rec_one_date);
        tvMedia= (TextView) findViewById(R.id.tv_new_rec_one_media);
        imageView= (NetworkImageView) findViewById(R.id.iv_new_rec_one_img);
        tvContent= (TextView) findViewById(R.id.tv_new_rec_one_content);
        Intent intent=getIntent();
        String receiveUrl=intent.getStringExtra("url");
        String receiveUr=intent.getStringExtra("ur");
       int position=intent.getExtras().getInt("position");

        String ur[]=receiveUr.split("\\?");
        final String image=intent.getStringExtra("img");

        Log.d("-----------", ur[0]);
        Log.d("***********", position+"");

        url="http://platform.sina.com.cn/news/news?app_key=2586208540&_version__=3.2.0.14&__os__=android&url="+
               ur[0]+"&extra_recommend=cre%3Dsptapp%26mod%3Df%26loc%3D"+position+""+"%26r%3D0%26rfunc%3D44&fields=createdatetime,title,"+
                "short_title,media,subject,column,author,content,images,video,comment,weibo,related_news&format=json&reservedtag"+
                "=HDSlide,WeiboList&related_short_title=y&client_token=866500020074540&client_deviceid=606f8e0559ca33cfa214e8e14c061c40";

        Log.d("+++++++++++", url);
        queue= Volley.newRequestQueue(this);
        final ImageLoader imageLoader=new ImageLoader(queue,new DoubleCache());

        GsonRequest<GsonNewsRecOneDec> request=new GsonRequest<GsonNewsRecOneDec>(url, GsonNewsRecOneDec.class, new Response.Listener<GsonNewsRecOneDec>() {
            @Override
            public void onResponse(GsonNewsRecOneDec gsonNewsRecOneDec) {
                Log.i("test",""+gsonNewsRecOneDec);
                int size=gsonNewsRecOneDec.getResult().getData().size();
                for (int i=0;i<size;i++){
                    String title=gsonNewsRecOneDec.getResult().getData().get(i).getTitle();
                    String createTime=gsonNewsRecOneDec.getResult().getData().get(i).getCreatedatetime();
                    String media=gsonNewsRecOneDec.getResult().getData().get(i).getMedia();
                    String content=gsonNewsRecOneDec.getResult().getData().get(i).getContent();
                    int comment_total=gsonNewsRecOneDec.getResult().getData().get(i).getComment().getTotal_count();


//                    Log.d("===========", title+"1111111");

                    tvCount.setText(comment_total+"评");
                    tvTitle.setText(title);
                    tvDate.setText(createTime);
                    tvMedia.setText(media);
                    tvContent.setText(Html.fromHtml(content));

                    imageView.setDefaultImageResId(R.mipmap.ic_main_tab_match_p);
                    imageView.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
                    imageView.setImageUrl(image,imageLoader);

                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();

            }
        });


       queue.add(request);

    }
}
